#Java - Code Snippets for '#String.length' - 2 code snippet(s) found |
|
Sample 1. Null or Empty String check using string length | |
|
String str= "";
if (str != null || str.length() != 0) {
// Do something
}
|
|
Like Feedback string null check String.length check empty string |
|
|
Sample 2. Find shortest and longest word in a string | |
|
public static void main(String[] args) {
String str = "We are not in Kansas anymore";
String shortestWord = "";
String longestWord = "";
int shortestWordLength = 1000;
int longestWordLength = 0;
String[] splitStr = str.split(" ");
for(String string: splitStr){
if(string.length() > longestWordLength){
longestWord = string;
longestWordLength = string.length();
} else if(string.length() < shortestWordLength){
shortestWord = string;
shortestWordLength = string.length();
}
}
System.out.println("Shortest Word :" + shortestWord);
System.out.println("Longest Word :" + longestWord);
}
|
|
Like Feedback string string.split |
|
|